package com.labot.mail;

import java.util.Properties;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import com.labot.types.ConstantsType;
import com.labot.webapp.listener.StartupListener;

public class CustomMailSender extends JavaMailSenderImpl {

	public CustomMailSender() {		
		super();
	}
	
	public void setConfiguration() {
		super.setHost(StartupListener.getConfigValue(ConstantsType.MAIL_HOST));
		try { super.setPort(Integer.parseInt(StartupListener.getConfigValue(ConstantsType.MAIL_PORT))); } catch (Exception e) { }
		super.setProtocol(StartupListener.getConfigValue(ConstantsType.MAIL_TRANSPORT_PROTOCOL));
		super.setUsername(StartupListener.getConfigValue(ConstantsType.MAIL_USERNAME));
		super.setPassword(StartupListener.getConfigValue(ConstantsType.MAIL_PASSWORD));
		super.setDefaultEncoding(StartupListener.getConfigValue(ConstantsType.MAIL_ENCODING));
//		
		Properties javaMailProperties = new Properties();
//		javaMailProperties.put(ConstantsType.MAIL_HOST.getPlaceHolderKey(), StartupListener.getConfigValue(ConstantsType.MAIL_HOST));
//		Integer port = 25;
//		try {			
//			port = Integer.parseInt(StartupListener.getConfigValue(ConstantsType.MAIL_PORT));
//		} catch (Exception e) {
//			//25
//		}
//		javaMailProperties.put(ConstantsType.MAIL_PORT.getPlaceHolderKey(), port);
//		javaMailProperties.put(ConstantsType.MAIL_TRANSPORT_PROTOCOL.getPlaceHolderKey(), StartupListener.getConfigValue(ConstantsType.MAIL_TRANSPORT_PROTOCOL));
//		javaMailProperties.put(ConstantsType.MAIL_USERNAME.getPlaceHolderKey(), StartupListener.getConfigValue(ConstantsType.MAIL_USERNAME));
//		javaMailProperties.put(ConstantsType.MAIL_PASSWORD.getPlaceHolderKey(), StartupListener.getConfigValue(ConstantsType.MAIL_PASSWORD));
//		javaMailProperties.put(ConstantsType.MAIL_ENCODING.getPlaceHolderKey(), StartupListener.getConfigValue(ConstantsType.MAIL_ENCODING));
//		javaMailProperties.put(ConstantsType.MAIL_HOST.getPlaceHolderKey(), StartupListener.getConfigValue(ConstantsType.MAIL_HOST));
		
		try {			
			if (StartupListener.getConfigValue(ConstantsType.MAIL_SMTP_AUTH).toLowerCase().startsWith("t")) {
				javaMailProperties.put(ConstantsType.MAIL_SMTP_AUTH.getPlaceHolderKey(), "true");
			} else {
				javaMailProperties.put(ConstantsType.MAIL_SMTP_AUTH.getPlaceHolderKey(), "false");
			}
		} catch (Exception e) {
			//false
		}
		try {			
			if (StartupListener.getConfigValue(ConstantsType.MAIL_START_TLS).toLowerCase().startsWith("t")) {
				javaMailProperties.put(ConstantsType.MAIL_START_TLS.getPlaceHolderKey(), "true");
			} else {
				javaMailProperties.put(ConstantsType.MAIL_START_TLS.getPlaceHolderKey(), "false");
			}
		} catch (Exception e) {
			// false
		}
		super.setJavaMailProperties(javaMailProperties);
		
		/**
		 <property name="username" value="XXX@gmail.com" />
		    <property name="password" value="XXX" />
		    <property name="javaMailProperties">
		    <props>
		        <prop key="mail.smtp.user" value="XXX@gmail.com" />
		        <prop key="mail.smtp.password" value="XXX" />
		        <prop key="mail.smtp.host">smtp.gmail.com</prop>
		        <prop key="mail.smtp.port">587</prop>
		        <prop key="mail.smtp.auth">true</prop>
		        <prop key="mail.smtp.starttls.enable">true</prop>
		    </props>
		</property>
	    **/
	}
}
